<?php

namespace Buu\Modules\RedisSetting\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\RedisInstance;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Support\MessageBag;
use Encore\Admin\Controllers\AdminController;

/**
 * description
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     weiyizheng
 * @date       2022/12/6
 * @time       4:19 下午
 */
class RedisInstanceController extends AdminController
{
    public $title = 'Redis实例';

    public function index(Content $content)
    {
        $content->breadcrumb([
            'text' => $this->title
        ]);
        return parent::index($content);
    }

    public function grid()
    {
        $grid = new Grid(new RedisInstance());
        $grid->paginate(50);
        $grid->disableBatchActions();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->column('id', 'ID')->sortable();
        $admin_name = Admin::user()->username;
        if (in_array($admin_name, ['congyukun','lishuang'])){
            $grid->column('name', '实例名称')->editable();
            $grid->column('desc', '描述')->editable();
        }else{
            $grid->column('name', '实例名称');
            $grid->column('desc', '描述');
        }

        $grid->column('host', '地址-端口')->display(function () {
            $html = '<div class="span-label" style="margin-bottom: 5px"><span class="label label-success"> 生产：' . $this->host . ":" . $this->port . '</span></div>';
            $html .= '<div class="span-label" style="margin-bottom: 5px"><span class="label label-info">测试：' . $this->test_host . ":" . $this->test_port . '</span></div>';
            $html .= '<div class="span-label" style="margin-bottom: 5px"><span class="label label-warning">开发：' . $this->dev_host . ":" . $this->dev_port . '</span></div>';
            return $html;
        });
//        $grid->column('user', '用户名')->display(function () {
//            $html = '<div class="span-label" style="margin-bottom: 5px"><span class="label label-success">' . $this->user . '</span></div>';
//            $html .= '<div class="span-label" style="margin-bottom: 5px"><span class="label label-info">' . $this->test_user . '</span></div>';
//            $html .= '<div class="span-label" style="margin-bottom: 5px"><span class="label label-warning">' . $this->dev_user . '</span></div>';
//            return $html;
//        });
//        $grid->disableActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
//			$actions->disableDelete();
			$actions->disableEdit();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new RedisInstance());
        $form->footer(function (Form\Footer $footer) {
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
            $footer->disableViewCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->text('name', '实例名称')->placeholder('请输入名称')->setWidth(4, 2)->required();
        $form->text('desc', '描述')->setWidth(4, 2);
        $form->text('host', '生产 - 实例地址')->setWidth(4, 2)->required();
        $form->number('port', '端口')->setWidth(4, 2)->default('6379')->required();
        $form->text('user', '用户名')->setWidth(4, 2);
        $form->password('password', '密码')->setWidth(4, 2);


        $form->text('test_host', '测试 - 实例地址')->setWidth(4, 2)->required();
        $form->number('test_port', '端口')->setWidth(4, 2)->default('6379')->required();
        $form->text('test_user', '用户名')->setWidth(4, 2);
        $form->password('test_password', '密码')->setWidth(4, 2);

        $form->text('dev_host', '开发 - 实例地址')->setWidth(4, 2)->required();
        $form->number('dev_port', '端口')->setWidth(4, 2)->default('6379')->required();
        $form->text('dev_user', '用户名')->setWidth(4, 2);
        $form->password('dev_password', '密码')->setWidth(4, 2);

        // 抛出错误信息
//        $form->saving(function ($form) {
//            $create = $form->isCreating();
//
//            if ($create) {
//                $onlyOne = RedisInstance::isOnlyOne($form->host);
//                if (!$onlyOne) {
//                    $error = new MessageBag([
//                        'title' => '请注意！！！',
//                        'message' => '请保证生产环境实例唯一性',
//                    ]);
//                    return back()->with(compact('error'))->withInput();
//                }
//            }
//        });
        $form->confirm('确定提交吗？');

        return $form;
    }
}